package com.kdtech.analyse.news.ulinix;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;

import com.kdtech.analyse.AnalyseNews;
import com.kdtech.analyse.Bbs.common.BbsAnalyseDiscuz;
import com.kdtech.entity.crawler.UrlMeta;

import com.kdtech.entity.data.NewsMeta;
import com.kdtech.utils.DateUtils;
import com.kdtech.utils.HtmlCleaner;
import com.kdtech.utils.StringUtils;

/**
 * Ts
 * @author Persh
 *
 */
public class EynekBizNewsAnalyse implements AnalyseNews {

	private static final String[] regex={
		"http://www.eynek.biz/index.php/[0-9\\-]*/[0-9\\-]*/[0-9]*-uncategorised/[0-9\\-]*",
		"http://www.eynek.biz/durmiz/forum.php\\?mod=viewthread&tid=[0-9]*.*"
	};


	
	public boolean isDetailPage(String url) {
		for (int i=0; i < regex.length; i++) {
			if (url.matches(regex[i])) {
				return true;
			}
		}
		return false;
	}

	
	public NewsMeta parserHtml(UrlMeta urlMeta) {
		NewsMeta news=new NewsMeta();
		if (urlMeta.getHtml() == null) {
		}
		String htmltxt=urlMeta.getHtml();
		String url=urlMeta.getUrl();

		news.setUrl(url);
		String title=null;
		String content=null;
		String author=null;
		Long date=null;
		Document doc=Jsoup.parse(htmltxt);

		if(url.contains("viewthread")){
			NewsMeta bbsMeta = new BbsAnalyseDiscuz().parserHtml(urlMeta);
			news.setTitle(bbsMeta.getTitle());
			news.setContent(bbsMeta.getContent());
			news.setAuthor(bbsMeta.getAuthor());
			news.setDate(bbsMeta.getDate());
			news.setClickNum(bbsMeta.getClickNum());
			news.setCommentNum(bbsMeta.getCommentNum());
		}else{
			title =doc.select("title").text();
			title = StringUtils.substringBefore(title, "-");
			content=HtmlCleaner.getContentHtml(url,doc.select("#main"));
			date=DateUtils.matchDate(doc.select(".published").text());
			news.setTitle(StringUtils.trimSpace(title));
			news.setContent(content);
			news.setAuthor(author);
			news.setDate(date);
		}

		return news;
	}

	

	

	
}
